package ru.amse.tsyganov.jumleditor.view.representation;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;

import ru.amse.tsyganov.jumleditor.graphicaleditor.Coordinates;
import ru.amse.tsyganov.jumleditor.model.ActivityGraph;
import ru.amse.tsyganov.jumleditor.model.Decision;
import ru.amse.tsyganov.jumleditor.properties.StringProperty;
import ru.amse.tsyganov.jumleditor.validator.Problem;
import ru.amse.tsyganov.jumleditor.validator.Validator;
import ru.amse.tsyganov.jumleditor.view.SelectedItems;
import ru.amse.tsyganov.jumleditor.view.Validity;
import ru.amse.tsyganov.jumleditor.view.ViewVisitor;
import ru.amse.tsyganov.jumleditor.view.preferences.Colors;
import ru.amse.tsyganov.jumleditor.view.preferences.Images;

public class DecisionView<T extends Decision> extends StateView<T> {

	private final static int DECISION_DEFAULT_WIDTH = 30;
	
	private final static int DECISION_DEFAULT_HEIGHT = 30;
	
	public DecisionView(T model, 
			ActivityGraphView<? extends ActivityGraph> activityGraphView) {
		
		super(model, activityGraphView);
		
		setWidth(DECISION_DEFAULT_WIDTH);
		setHeight(DECISION_DEFAULT_HEIGHT);
		
	}
	
	@Override
	public void accept(ViewVisitor v) {
		v.visitDecisionView(this);
		visitTransitions(v);
	}
	
	public Color getFillColor() {
		return Colors.getGreenColor();
	}

	public String getClassName() {
		return "DecisionView";
	}

	@Override
	public List<Problem> getInnerProblems() {
		ArrayList<Problem> problems = new ArrayList<Problem>();
		if (getModel().getStateVertex().incomingTransitionsSize() == 0) {
			SelectedItems problemSpace = new SelectedItems();
			problemSpace.selectedStatesAdd(this);
			
			String message = Validator.ERROR +  
				": number of incoming transitions shouldn't be zero";
			
			problems.add(new Problem(message, 
					problemSpace, Images.getErrorImage()));
			
			setValidity(Validity.ERROR);
		}
		
		if (getModel().getStateVertex().outgoingTransitionsSize() == 0) {
			SelectedItems problemSpace = new SelectedItems();
			problemSpace.selectedStatesAdd(this);
			
			String message = Validator.ERROR +  
				": number of outgoing transitions shouldn't be zero";
			
			problems.add(new Problem(message, 
					problemSpace, Images.getErrorImage()));
			
			setValidity(Validity.ERROR);
		}
		
		if (getModel().getStateVertex().outgoingTransitionsSize() == 1 &&
				getModel().getStateVertex().incomingTransitionsSize() == 1) {
			SelectedItems problemSpace = new SelectedItems();
			problemSpace.selectedStatesAdd(this);
			
			String message = Validator.WARNING +  
				": this state is useless";
			
			problems.add(new Problem(message, 
					problemSpace, Images.getWarningImage()));
			
			setValidity(Validity.WARNING);
		}
		return problems;
	}

	@Override
	public void paintFigure(GC gc, Coordinates coordinates) {
		assert gc != null;
		
		int[] polygon = 
				new int[] {
				coordinates.fromModelXToScreenX(getX() + getWidth() / 2), 
				coordinates.fromModelYToScreenY(getY()),
				coordinates.fromModelXToScreenX(getX() + getWidth()), 
				coordinates.fromModelYToScreenY(getY() + getHeight() / 2),
				coordinates.fromModelXToScreenX(getX() + getWidth() / 2), 
				coordinates.fromModelYToScreenY(getY() + getHeight()),
				coordinates.fromModelXToScreenX(getX()), 
				coordinates.fromModelYToScreenY(getY() + getHeight() / 2)
				};
				
				
		gc.setBackground(getFillColor());
		gc.fillPolygon(polygon);
		
		gc.setForeground(getValidity().getColor());
		gc.setLineWidth(getValidity().getLineWidth());
		gc.drawPolygon(polygon);
		
		gc.setLineWidth(0);
	}
	
	@Override
	public StringProperty getMainProperty() {
		return null;
	}
}
